package servlet;

import pojo.Menu;
import pojo.Role;
import pojo.Users;
import service.MenuService;
import service.MenuServiceImpl;
import service.RoleService;
import service.RoleServiceImpl;
import utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = ("/role/roleservlet"))
public class RoleServlet extends HttpServlet {
    RoleService roleService = new RoleServiceImpl();
    MenuService menuService = new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("insert".equals(method)){
            addRole(req, resp);
        }else if ("update".equals(method)){
            updateRole(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }else if ("rolelist".equals(method)){
          rolelist(req,resp);
        }else if ("menulist".equals(method)){
            menulist(req,resp);
        }else {
            findbyid(req,resp);
        }
    }
    //查询role列表
    protected  void rolelist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<Role> roleList = null;
        PageUtils pageUtils = new PageUtils();
        //模糊查询
        //分页数据
        //页码值
        String pageIndex = req.getParameter("pageIndex");
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
        roleList = roleService.selectALl(index, pageUtils.getPageSize());
        //获取总页数
        int totalPage = 0;
        totalPage = roleService.total()%5>0?
                roleService.total()/5+1:
                roleService.total()/5;
        req.setAttribute("total",roleService.total());
        //System.out.println(usersList);
        req.setAttribute("roleList",roleList);
        req.setAttribute("pageIndex",index);
        req.setAttribute("totalPage",totalPage);
        req.getRequestDispatcher("/power/role/list.jsp").forward(req,resp);
    }
    //查询菜单列表
    protected  void menulist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<Menu> menus = menuService.selectAll();
        req.setAttribute("menus",menus);
        req.getRequestDispatcher("/power/role/add.jsp").forward(req,resp);
    }
    //新增角色
    protected  void addRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rolename = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = roleService.addRole(rolename, state, menuids);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('新增成功');location.href='/role/roleservlet?method=rolelist'</script>");
        }else {
            writer.println("<script>alert('新增失败');location.href='/role/roleservlet?method=menulist'</script>");
        }
    }
    //修改角色
    protected  void updateRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        addRole(req,resp);
    }
    //通过角色id查询角色信息
    protected  void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String roleid = req.getParameter("roleid");
        Role role = roleService.findbyid(Integer.parseInt(roleid));
        List<Menu> menuList = menuService.selectAll();
        req.setAttribute("role",role);
        req.setAttribute("menus",menuList);
        delete(req,resp);
        req.getRequestDispatcher("/power/role/edit.jsp").forward(req,resp);
    }
    //通过id删除角色信息
    protected  void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String roleid = req.getParameter("roleid");
        int i = roleService.deletebyid(Integer.parseInt(roleid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('删除成功');location.href='/role/roleservlet?method=rolelist'</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/role/roleservlet?method=rolelist‘</script>");
        }
    }
}
